// sub_renyuanguanli/renyuanlist/renyuanlist.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    grade: "",
    bankInfo: {},
    shortname: "",
    searchValue: "",
    isfirstAdmin: false,
    isManage: false,
    list: [],
    pageNum: 1,
    pageSize: "20",
    selectCheckbox: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.id) {
      this.setData({
        id: options.id,
        grade: options.grade,
      })
      this.getList()
    }
    if (options.shortname) {
      this.setData({
        shortname: options.shortname
      })
    }
    this.queerBankInfo()

  },
  onSearch(e) {
    console.log(e);
    this.setData({
      searchValue: e.detail,
    })
    this.readygetList()
  },

  selectChange(e) {
    this.setData({
      selectCheckbox: e.detail,
    });
  },
  cellCheckToggle(e) {
    const {
      index
    } = e.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },
  noop() {},
  // method
  getList() {
    getApp().globalData.request(
      "/corpVisit/queryBankUserList", {
        name: this.data.searchValue,
        bankId: this.data.id,
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize
      },
      "POST"
    ).then(res => {
      console.log(res);
      this.setData({
        list: this.data.list.concat(res.data.list)
      })
    })
  },
  // 判断是否是一级管理员
  isFiradmin() {
    let arr = wx.getStorageSync("userInfo").corpRole;
    // let result = arr.find((item,index)=>{
    //   return item == "fir_admin"
    // })
    if (arr.indexOf("fir_admin") !== -1) {
      this.setData({
        isfirstAdmin: true,
      })
    } else {
      this.setData({
        isfirstAdmin: false,
      })
    }
  },
  // 查询银行信息
  // /query_user_bank_info
  queerBankInfo() {
    getApp().globalData.request(
      "/corpVisit/query_user_bank_info", {bankId:this.data.id},
      "POST"
    ).then(res => {
      console.log(res);
      if (res.code == 200) {
        this.setData({
          bankInfo: res.data
        })
      }
    })
  },
  // 管理
  toManageClick() {
    this.setData({
      isManage: true,
      selectCheckbox: []
    })
  },
  noManageClick() {
    this.setData({
      isManage: false,
    })
  },
  // 去详情 
  toDetailClick(e) {
    wx.navigateTo({
      url: `/sub_renyuanguanli/person_detail/person_detail?id=${e.currentTarget.dataset.uid}&shortname=${this.data.shortname}`,
    })
  },
  // 去添加
  toAddClick() {
    wx.navigateTo({
      url: `/sub_renyuanguanli/person_add/person_add?id=${this.data.bankInfo.branchBankId}&name=${this.data.bankInfo.branchShortName}`
    })
  },
  // 批量删除
  deleteClick() {
    console.log(this.data.selectCheckbox);
    if (this.data.selectCheckbox.length > 0) {
      getApp().globalData.request(
        "/user/delete_batch", this.data.selectCheckbox,
        "POST"
      ).then(res => {
        console.log(res);
        if (res.code == 200) {
          this.setData({
            selectCheckbox: []
          })
          this.readygetList()
        }
      })
    } else {
      wx.showToast({
        title: '请先选择您要删除的人员！',
        icon: "none"
      })
    }
  },
  readygetList() {
    this.setData({
      pageNum: "1",
      list: []
    })
    this.getList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  scrollBottom() {
    this.setData({
      pageNum: this.data.pageNum*1 + 1,
    })
    console.log(this.data.pageNum);
    this.getList()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.refresh) {
      this.readygetList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})